﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
//.NET socket libraries
using System.Net;
using System.Net.Sockets;
//for threading inclu threads safe method
using System.Threading;
using System.Net.NetworkInformation;
using System.Diagnostics;


namespace EITSurferInfinityLib
{
    public class Swisstom
    {
        protected Ethernet DataPort;
        protected Ethernet MsgPort;

        protected String IPAddress;
        protected string PreviousDeviceIP;
        protected bool Connected;
        protected bool DeviceDetected;

        protected int DataSize;

        protected int code;


        public Swisstom()
        {
            DataPort = new Ethernet();
            MsgPort = new Ethernet();
            ApplyDefaultPort();
            PreviousDeviceIP = "";
            Connected = false;
        }

        protected void ProgramParameter(string[] ParameterName, string [] Values)
        {
            if (IsConnected())
            {
                int length=ParameterName.Length;
                for (int i = 0; i < length; i++)
                {
                    Send("set" + ParameterName[i] + Values[i]+"\n");
                }
            }
            else
            {
                Debug.WriteLine("NOT CONNECTED");
            }
        }


        #region port10000
        public void ConnectEventMsgPort()
        { }
        public void Send(string s)
        {
            MsgPort.Send(s);
        }
        #endregion

        #region port10001
        public void ConnectEventDataPort()
        { }
        public void PullOneFrame()
        {
            //writes directly into the DataManager
        }
        public int GetCode()
        {
            Stop();
            Thread.Sleep(100);
            DataPort.ClearBuffer();
            MsgPort.ClearBuffer();
            Start(1);
            Thread.Sleep(100);
            byte[] buffer = new byte[4];
            for (int i = 0; i < 4; i++)
            {
                buffer[i] = DataPort.DataByte[i];
            }

            code = BitConverter.ToInt32(buffer, 0);

            Debug.WriteLine("code is:" + code.ToString());
            return code;
        }
        #endregion

        #region commands

        public void Start()
        {
            Send("start");
        }

        public void Start(int NumberFrame)
        {
            Send("start " + NumberFrame.ToString());
        }

        public void Stop()
        {
            Send("stop");
        }

        #endregion

        #region Connection
        protected void ApplyDefaultPort()
        {
            DataPort.Port = 10001;
            MsgPort.Port = 10000;
        }
        protected void AutoDetect()
        {
            EthernetHelper helper = new EthernetHelper("192.168.1.254");
            helper.TestText = "SBC";
            helper.TestType = "Contains";
            helper.OnConnected += new EthernetHelper.OnConnectedHandler(helper_OnConnected);
            helper.OnConnectionMessage += new EthernetHelper.OnConnectionMessageHandler(helper_OnConnectionMessage);
            helper.AutoDetect();
        }

        protected virtual void helper_OnConnectionMessage(object sender, EventArgsMsg e)
        {
           // throw new NotImplementedException();
        }
        protected void helper_OnConnected(Object sender, EventArgsMsg e)
        {
            IPAddress = e.Message;
            DeviceDetected = true;
            ConnectAllPort();
        }
        public bool IsConnected()
        {
            if (MsgPort.IsConnected() && DataPort.IsConnected())
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public void Connect()
        {
            AutoDetect();
        }
        public virtual bool ConnectAPort(ref Ethernet myPort)
        {
            bool r = true;
            try
            {
                if (myPort != null)
                {
                    myPort.Connect();

                    Debug.WriteLine("Connected To: " + myPort.ToString());
                }
            }
            catch (Exception e)
            {
                r = false;
                Debug.WriteLine(e);
            }
            return r;
        }
        public virtual void ConnectP2P()
        {
            IPAddress = "192.168.1.254";
            DeviceDetected = true;
            ConnectAllPort();
        }
        public void Disconnect()
        {
            Debug.WriteLine("start debug");
            if (MsgPort != null && MsgPort.IsConnected())
            {
                MsgPort.Disconnect();
                Debug.WriteLine("Msg port disconnected");
            }
            if (DataPort != null && DataPort.IsConnected())
            {
                DataPort.Disconnect();
                Debug.WriteLine("data port diconnected");
            }
        }
        public virtual  void ConnectAllPort()
        {
            DataPort = new Ethernet();
            MsgPort = new Ethernet();
            
            DataPort.SetIPAddress(IPAddress);
            MsgPort.SetIPAddress(IPAddress);
            ApplyDefaultPort();

            if (DeviceDetected)
            {
                if (ConnectAPort(ref MsgPort))
                {
                    PreviousDeviceIP = IPAddress;
                    if (ConnectAPort(ref DataPort))
                    {
                        Debug.WriteLine("DEVICE is CONNECTED!");
                    }
                }
            }


            DataPort.OnReceived += new Ethernet.OnReceivedHandler(DataPort_OnReceived);
            MsgPort.OnReceived += new Ethernet.OnReceivedHandler(MsgPort_OnReceived);

        }

        private void DataPort_OnReceived(Object sender, EventArgs e)
        {

        }
        private void MsgPort_OnReceived(Object sender, EventArgs e)
        {

        }


    }
        #endregion
}

